﻿using System;
using System.Linq;
using Loki;
using Luna.Model.Accounting;

namespace Luna.UI.Accounting
{
    public class AccountingCategoryGraphViewModel : AccountingGraph
    {
        private LokiCollection<GraphPoint<string, decimal>> _SourcePoints;

        public override object DataSource
        {
            get { return _SourcePoints; }
        }

        public AccountingCategoryGraphViewModel()
        {
            _SourcePoints = new LokiCollection<GraphPoint<string, decimal>>();
        }

        protected override void ReloadGraph(object P_Sender, EventArgs e)
        {
            if (AccountID.HasValue)
            {
                IAccount L_Account = ApplicationModel.Accounting.Accounts[AccountID.Value];
                var L_Points = L_Account.Operations.Where(x => x.ExecutionDate > StartDate && x.ExecutionDate <= EndDate).Where(x => x.Amount < 0);
                _SourcePoints.Clear();

                // first point
                foreach (var L_Group in L_Points.GroupBy(x => x.Category))
                {
                    _SourcePoints.Add(new GraphPoint<string, decimal>() { Argument = L_Group.Key.Name, Value = L_Group.Sum(x => Math.Abs(x.Amount)) });
                }
            }
        }
    }
}
